package com.ztom.daily.n2022.m3;

import java.util.ArrayList;
import java.util.List;

/**
 * 适合打劫银行的日子
 * <p>
 * https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/
 *
 * @author ZhangTao
 */
public class Day06GoodDaysToRobBank {

    /**
     * 其实就是找到一个位置: 左右两边都有连续 time 个数大于等于当前的值
     */
    public List<Integer> goodDaysToRobBank(int[] security, int time) {
        List<Integer> res = new ArrayList<>();
        if (security == null || security.length == 0 || time < 0) {
            return res;
        }
        if (time == 0) {
            for (int i = 0; i < security.length; i++) {
                res.add(i);
            }
            return res;
        }

        // 记录左右分别连续有几个数大于等当前位置的值
        int l = 0;
        int r = 0;

        for (int i = 1; i < security.length - time; i++) {
            if (security[i - 1] >= security[i]) {
                l++;
            } else {
                l = 0;
            }
            if (security[i + time - 1] <= security[i + time]) {
                r++;
            } else {
                r = 0;
            }

            if (l >= time && r >= time) {
                res.add(i);
            }
        }

        return res;
    }
}
